package com.dailywork.basepro.thread.message;

public class ChangLock {

    private String lock = "lock";

    private void method() {
        synchronized (lock) { // ������û����û����,��Ϊ���ı���
            try {
                System.out.println("��ǰ�߳�: " + Thread.currentThread().getName() + "��ʼ");
                lock = "change lock";
                Thread.sleep(2000);
                System.out.println("��ǰ�߳�: " + Thread.currentThread().getName() + "����");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final ChangLock changLock = new ChangLock();
        Thread t1 =
                new Thread(
                        new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                changLock.method();
                            }
                        },
                        "t1");

        Thread t2 =
                new Thread(
                        new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                changLock.method();
                            }
                        },
                        "t2");

        t1.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t2.start();
    }
}
